home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / WalkT / draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-22  |  4.1 KB  |  135 lines

  1. /**********************************************************************/
  2. /* draw.c                                                             */
  3. /*                                                                    */
  4. /* Routines to draw meshes of polygons using GOURAUD, FLAT shading or */
  5. /* wireframe.                                                         */
  6. /*                                                                    */
  7. /* Copyright (C) 1992, Bernard Kwok                                   */
  8. /* All rights reserved.                                               */
  9. /* Revision 1.0                                                       */
  10. /* May, 1992                                                          *
  11. /**********************************************************************/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <gl/gl.h>
  15. #include <gl/device.h>
  16. #include <fmclient.h>
  17. #define IRIS4D 1
  18. #include "geo.h"
  19. #include "walk.h"
  20. #include "scene.h"
  21.  
  22. extern SMesh Scene;
  23. /**********************************************************************/
  24. /* Convert Spectra into RGB triples                                   */
  25. /* Assume first 3 samples of spectra are r,g,b.                       */
  26. /**********************************************************************/
  27. void SpectraToRGB(pptr)
  28.      SPolygon *pptr;
  29. {
  30.   int c,v;
  31.  
  32.   /* Scale the intensity */
  33.   for (c=0;c<MAX_SPECTRA_SAMPLES;c++) {
  34.     for(v=0;v<pptr->numVert;v++) {
  35.       if (pptr->vtx_B[v][c] > 0.9999)
  36.     pptr->vtx_B[v][c] = 0.9999; 
  37.     }
  38.     if (pptr->B[c] > 0.9999)
  39.       pptr->B[c] = 0.9999;
  40.   }
  41. }
  42.  
  43. /**********************************************************************/
  44. /* Draw a polygon in a mesh for an object */
  45. /**********************************************************************/
  46. void Draw_Polygon(pptr)
  47.      SPolygon *pptr;
  48. {
  49.   int i, j;
  50.   float n[3], v[3], c[3];
  51.   int vin[4];                    /* Order of vertices to draw */
  52.  
  53.   /* Set default vertex drawing order */
  54.   vin[0] = 0; vin[1] = 1; vin[2] = 2; vin[3] = 3;
  55.  
  56.   /* Draw triangulated scene. Only need to triangulate quads */
  57.   if (Option.shadeprim == SHADE_TRIA) {
  58.     if (pptr->class == PATCH) { 
  59.  
  60.       /* Triangulate quad by splitting along 2 vertices with most
  61.      similiar colour */
  62.       if (fabs(pptr->vtx_B[0] - pptr->vtx_B[2]) >
  63.       fabs(pptr->vtx_B[3] - pptr->vtx_B[1])) {
  64.     vin[0] = 0; vin[1] = 3; vin[2] = 1; vin[3] = 2; 
  65.       } else {
  66.     vin[0] = 1; vin[1] = 2; vin[2] = 0; vin[3] = 3; 
  67.       }
  68.     }
  69.   } 
  70.  
  71.   if (Option.shadeprim == NO_SHADE) {
  72.     bgnclosedline(); 
  73.   } 
  74.   else if (Option.shadeprim == SHADE_TRIA) {
  75.     if (pptr->class == PATCH) bgntmesh();
  76.     else bgnpolygon();
  77.   } else if (Option.shadeprim == SHADE_QUAD) {
  78.     bgnpolygon();
  79.   }
  80.   
  81.   for(i=0;i<pptr->numVert;i++) {
  82.     if (Option.shademodel == GOURAUD) {
  83.       c[0] = pptr->vtx_B[vin[i]][0]; c[1] = pptr->vtx_B[vin[i]][1];
  84.       c[2] = pptr->vtx_B[vin[i]][2];
  85.     } else if ((Option.shadeBW == TRUE) && (Option.shadeprim == NO_SHADE)) {
  86.       c[0] = 1.0; c[1] = 1.0; c[2] = 1.0;
  87.     } else {
  88.       c[0] = pptr->B[0]; c[1] = pptr->B[1]; c[2] = pptr->B[2];
  89.     }
  90.     c[3] = 0.0;
  91.  
  92.     if (Option.RGBscale != 1.0) {
  93.       for (j=0;j<3;j++)    
  94.     c[j] = c[j] * Option.RGBscale;
  95.       if (c[j] > 1.0) c[j] = 1.0;
  96.     }
  97.     
  98.     if (Option.shadeprim != NO_SHADE) {
  99.       n[0] = pptr->normal[vin[i]].x; n[1] = pptr->normal[vin[i]].y; 
  100.       n[2] = pptr->normal[vin[i]].z;
  101.       n3f(n);
  102.     }
  103.     
  104.     v[0] = pptr->vertex[vin[i]].x; v[1] = pptr->vertex[vin[i]].y;
  105.     v[2] = pptr->vertex[vin[i]].z;
  106.     c4f(c);
  107.     v3f(v);
  108.   }
  109.  
  110.   if (Option.shadeprim == NO_SHADE) {
  111.     endclosedline(); 
  112.   } else if (Option.shadeprim == SHADE_TRIA) {
  113.     if (pptr->class == PATCH) endtmesh();
  114.     else endpolygon();
  115.   } else if (Option.shadeprim == SHADE_QUAD) {
  116.     endpolygon();
  117.   }
  118. }
  119.  
  120. /**********************************************************************/
  121. /* Draw a polygonal mesh of an object */
  122. /**********************************************************************/
  123. void Draw_Mesh()
  124. {
  125.   int i;
  126.   SPolygon *pptr;
  127.  
  128.   /*  if (Option.shadeprim == NO_SHADE)
  129.       depthcue(1); */
  130.   for (i=0, pptr = Scene.polys; i<(Scene.num_polys);i++, pptr++)
  131.     Draw_Polygon(pptr);
  132.   /* if (Option.shadeprim == NO_SHADE)
  133.      depthcue(0); */
  134. }
  135.